#include <arch/types.h>

void *memset(void *dstpp, int c, size_t len) {
	long int dstp = (long int) dstpp;

	if (len >= 8) {
		size_t xlen;
		dword cccc;

		cccc = (unsigned char) c;
		cccc |= cccc << 8;
		cccc |= cccc << 16;

		/* There are at least some bytes to set.
		   No need to test for LEN == 0 in this alignment loop.  */
		while (dstp % 4 != 0) {
			((byte *) dstp)[0] = c;
			dstp += 1;
			len -= 1;
		}

		/* Write 8 `dword' per iteration until less than 8 `dword' remain.  */
		xlen = len / (4 * 8);
		while (xlen > 0) {
			((dword *) dstp)[0] = cccc;
			((dword *) dstp)[1] = cccc;
			((dword *) dstp)[2] = cccc;
			((dword *) dstp)[3] = cccc;
			((dword *) dstp)[4] = cccc;
			((dword *) dstp)[5] = cccc;
			((dword *) dstp)[6] = cccc;
			((dword *) dstp)[7] = cccc;
			dstp += 8 * 4;
			xlen -= 1;
		}
		len %= 4 * 8;

		/* Write 1 `dword' per iteration until less than OPSIZ bytes remain.  */
		xlen = len / 4;
		while (xlen > 0) {
			((dword *) dstp)[0] = cccc;
			dstp += 4;
			xlen -= 1;
		}
		len %= 4;
	}

	/* Write the last few bytes.  */
	while (len > 0) {
		((byte *) dstp)[0] = c;
		dstp += 1;
		len -= 1;
	}
	return dstpp;
}
